package com.jwell.bigdata.ui.login

import android.content.pm.ActivityInfo
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.Menu
import android.view.View
import android.webkit.WebView
import com.jwell.bigdata.R
import com.jwell.bigdata.base.MVPBaseActivity
import com.jwell.bigdata.comment.ConstValue
import com.jwell.bigdata.contract.LoginContract
import com.jwell.bigdata.presenter.LoginPresenter
import com.jwell.bigdata.ui.menu.MenuActivity
import com.jwell.bigdata.ui.web.WebActivity
import com.jwell.bigdata.utils.SPUtils
import com.jwell.bigdata.utils.ToastUtils
import kotlinx.android.synthetic.main.activity_login.*

class LoginActivity: MVPBaseActivity<LoginPresenter>(),LoginContract.View {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        requestedOrientation=ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        setContentView(R.layout.activity_login)
        initView()
        initEvent()
        showProgress("",false)
        presenter?.checkLogin()
    }

    private fun initView() {
        et_account.setText(SPUtils.getString(this,ConstValue.ACCOUNT,""))
        ib_account_delete.visibility=if (et_account.text.toString().isEmpty()) View.GONE else View.VISIBLE
    }

    private fun initEvent() {
        et_account.addTextChangedListener(object : TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                ib_account_delete.visibility=if (et_account.text.toString().isEmpty()) View.GONE else View.VISIBLE
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
        et_passWord.addTextChangedListener(object : TextWatcher{
            override fun afterTextChanged(s: Editable?) {
                ib_pass_delete.visibility=if (et_passWord.text.toString().isEmpty()) View.GONE else View.VISIBLE
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
        ib_account_delete.setOnClickListener {
            et_account.setText("")
        }
        ib_pass_delete.setOnClickListener {
            et_passWord.setText("")
        }
        btn_login.setOnClickListener {
            if(et_account.text.toString().isEmpty())
            {
                ToastUtils.showCenterToast(LoginActivity@this,"请输入账号")
                return@setOnClickListener
            }
            if(et_passWord.text.toString().isEmpty())
            {
                ToastUtils.showCenterToast(LoginActivity@this,"请输入密码")
                return@setOnClickListener
            }
            showProgress("登录中，请稍后",false)
          presenter?.login(et_account.text.toString(),et_passWord.text.toString())
        }
    }

    override fun checkLogin(isLogin: Boolean,cookieString:String) {
        if(isLogin)
        {
            MenuActivity.startActivity(cookieString,this)
            finish()
        }else
        {
            ToastUtils.showCenterToast(this,"登陆失败，请重新登陆")
        }
    }

    override fun loginSuccess(cookString:String) {
        MenuActivity.startActivity(cookString,this)
        finish()
    }

    override fun createPresenter()= LoginPresenter()


}